package io.mft.runtime.function;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.mft.api.domain.Api;
import io.mft.commons.function.ExecuteReturn;
import lombok.Getter;

/**
 * API 执行对象
 * 
 * @说明：TODO
 * @类型名称：ApiExecutorObject
 * @创建者: 胡常建
 * @创建时间: 2021年2月21日 下午5:43:59
 * @修改者: 胡常建
 * @修改时间: 2021年2月21日 下午5:43:59
 */
@Getter
public class ApiExecutorObject {
	Api api;
	FunctionExecutor functionExecutor;
	FunctionExecutor[] regulationExecutors;
	Map<String, FunctionExecutor> regulationExecutorMap = new ConcurrentHashMap<String, FunctionExecutor>();

	public ApiExecutorObject(Api api, FunctionExecutor functionExecutor, FunctionExecutor... regulationExecutors) {
		this.api = api;
		this.functionExecutor = functionExecutor;
		this.regulationExecutors = regulationExecutors;
		for (FunctionExecutor regulationExecutor : regulationExecutors) {
			regulationExecutorMap.put(regulationExecutor.getKey(), regulationExecutor);
		}
	}

	@SuppressWarnings("unchecked")
	public <T extends FunctionExecutor> T getRegulationExecutor(String key) {
		FunctionExecutor functionExecutor = regulationExecutorMap.get(key);
		T t = (T) functionExecutor;
		return t;
	}

	public ExecuteReturn execute(ApiFunctionRequest request,ApiFunctionResponse response) {
		ApiContext context = new ApiContext(this, request,response);
		return functionExecutor.doExecute(context);
	}
}
